<template>
  <div>
    <section class="all-comment">
      <div class="type" v-if="hotComments.length != 0">精彩评论</div>
      <div
        v-for="item in hotComments"
        class="each-comment"
        :key="item.commentId"
      >
        <div class="each-comment-avatar" @click="goUser(item.user.userId)">
          <img v-lazy="item.user.avatarUrl" />
        </div>
        <div class="each-comment-msg">
          <div class="each-comment-nickname">
            {{ item.user.nickname }}
          </div>
          <div class="each-comment-time">
            <div class="time">
              {{ new Date(item.time).getMonth() + 1 }}月{{
                new Date(item.time).getDate()
              }}日
            </div>
            <div
              @click="likeComment(id, item.commentId)"
              class="comment-likeicon"
            >
              {{ item.likedCount }}
              <span class="icon-点赞"></span>
            </div>
          </div>
          <div class="each-comment-content">
            {{ item.content }}
          </div>
        </div>
      </div>
    </section>
    <section class="all-comment">
      <div class="type" v-if="comments.length != 0">最新评论</div>
      <div v-for="item in comments" :key="item.commentId" class="each-comment">
        <div class="each-comment-avatar" @click="goUser(item.user.userId)">
          <img v-lazy="item.user.avatarUrl" />
        </div>
        <div class="each-comment-msg">
          <div class="each-comment-nickname">
            {{ item.user.nickname }}
          </div>
          <div class="each-comment-time">
            <div class="time">
              {{ new Date(item.time).getMonth() + 1 }}月{{
                new Date(item.time).getDate()
              }}日
            </div>
            <div
              @click="likeComment(id, item.commentId)"
              class="comment-likeicon"
            >
              <span v-if="item.likedCount != 0">{{ item.likedCount }}</span>
              <span class="icon-点赞"></span>
            </div>
          </div>
          <div class="each-comment-content">
            {{ item.content }}
          </div>
        </div>
      </div>
    </section>
  </div>
</template>

<script lang="ts">
import { APIType, CommentObjectType, LikeType } from "@/api";
import { CommentType, CommonIdType } from "@/model/id-or-enum.model";
import { CommentInfo } from "@/model/music-info.model";
import { Vue, Component, Prop } from "vue-property-decorator";
import Router, { Route } from "vue-router";
import { namespace } from "vuex-class";
const AppModule = namespace("app");
@Component({})
export default class CommentComponent extends Vue {
  @Prop({ default: [] })
  comments: CommentInfo[];
  @Prop({ default: [] })
  hotComments: CommentInfo[];

  $api: APIType;
  $router: Router;
  $route: Route;

  @AppModule.State("commentType")
  commentType: CommentType;

  get id() {
    return this.$route.params.id;
  }

  get commentObjectType(): CommentObjectType {
    switch (this.commentType) {
      case CommentType.ToSong:
        return CommentObjectType.Music;
      case CommentType.ToList:
        return CommentObjectType.PlayList;
      case CommentType.ToAlbum:
        return CommentObjectType.Album;
      case CommentType.ToMV:
        return CommentObjectType.MV;
    }
    return null;
  }

  likeComment(
    id: CommonIdType,
    commendId: CommonIdType,
    likeType: LikeType = 1,
    type?: CommentObjectType
  ) {
    // console.log(this.$route)
    type = type || this.commentObjectType;
    this.$api.setLike(id, commendId, likeType, type).then();
  }
  goUser(id: CommonIdType) {
    this.$router.push({ path: `/userHome/${id}` }).catch((err) => err);
  }
}
</script>

<style lang="scss" scoped>
.all-comment {
  background-color: #f9fafc;
  .type {
    font-size: 0.25rem;
    color: #666;
    height: 0.5rem;
    line-height: 0.5rem;
    background-color: #f9fafc;
    padding: 0 0.1rem;
  }
  .each-comment {
    display: flex;
    padding: 0.1rem 0;
    .each-comment-avatar {
      width: 0.8rem;
      height: 0.8rem;
      text-align: center;
      img {
        width: 0.6rem;
        height: 0.6rem;
        border-radius: 50%;
      }
    }
    .each-comment-msg {
      flex: 1;
      border-bottom: 1px solid #f9fafc;
      .each-comment-nickname {
        font-size: 0.16rem;
      }
      .each-comment-time {
        font-size: 0.12rem;
        display: flex;
        justify-content: space-between;
        color: #666;
        padding-right: 0.1rem;
        .time {
          font-size: 0.12rem;
        }
        .comment-likeicon {
          width: 1rem;
          text-align: right;
        }
      }
      .each-comment-content {
        font-size: 0.18rem;
        color: #333;
        margin-bottom: 0.2rem;
      }
    }
  }
}
</style>
